<?php

// $Id$

require_once('../../../../boot.inc.php');
boot('my_site_adm');

// discarding output buffering
ob_end_clean();

require_once(TANGRA_MAIN_DIR.'filesystem_toolbox/filesystem_functions.inc.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_attachments_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/attachment/tcms_attachment_dbc.class.php');

//session_start();


$context = new Web_Context();

if ($context->exists_in_get('fileid')) {
	$settings_attachments = new TCMS_Settings_Attachments_DBC();
	$dbc = $_SITE_DBC;
	if (!$settings_attachments->load_by_id($dbc, 1)) {
		throw new Tangra_Module_Exception('Cannot load images settings.');
	}


	$attachment_id = trim($context->get_from_get('fileid'));

	$attachment = new TCMS_Attachment_DBC();
	if ($attachment->load_by_id($dbc, $attachment_id)) {
		$filename = $WSC->get_uploads_dir().$settings_attachments->get_upload_path().$attachment->get_filename().'_'.$attachment->get_orig_filename();
		$orig_filename = $attachment->get_orig_filename();

		if (file_exists($filename)) {
			header("Cache-Control: public, must-revalidate");
			header("Pragma: hack"); // WTF? oh well, it works...
			header("Content-Type: application/octet-stream");
			header("Content-Length: " .(string)(filesize($filename)) );
			header('Content-Disposition: attachment; filename="'.$orig_filename.'"');
			header("Content-Transfer-Encoding: binary\n");
			readfile($filename);

		} else {
			printbr('Cannot find this file.');
		}
	} else {
		printbr('Cannot load this attachement');
	}

}



